﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HomeAutomation.modem
{
    class ModemFactory
    {
        public static Modem getModem(bool isTestMode){

            if (isTestMode)
            {
                return null;
            }
            else
            {
                return getPlmModem();
            }

        }

        private static Modem getPlmModem(){
            int MAX_COM_PORT = 99;
            int plmComPort = 0;

            Modem plm = new PlmModem("COM" + plmComPort);
            //get the right com port (if we can find one):
            plm.Receive();
            while (plm.Error() && plmComPort < MAX_COM_PORT)
            {
                //log("error connecting to COM" + plmComPort);
                plm = new PlmModem("COM" + ++plmComPort);
                plm.Receive();
            }

            if (plm.Error)
            {
                log("can't connect to a com port! exiting in 60 seconds!");
                Thread.Sleep(1000 * 60);
                Application.Exit();
            }
            else
            {
                log("connecting to COM" + plmComPort);
            }

            return plm;
        }
    }
}
